import os.path
from django.conf.urls.defaults import *
from django.contrib.comments.models import FreeComment

import settings
from biologeek.redirections import redirect
from biologeek.sitemaps import sitemaps
from biologeek.journal.models import Post
from biologeek.bistrot.models import Thought
from biologeek.journal.views import view_tag, add_tag, redirect_to_archives, archives_month, archives
from biologeek.feeds import RSSFeed, AtomFeed


## Misc
urlpatterns = patterns('django.views.generic.simple',
    (r'^$',             'direct_to_template', {'template': 'home.html'}),
    (r'^abonnement/$',  'direct_to_template', {'template': 'abonnement.html'}),
    (r'^contact/$',     'direct_to_template', {'template': 'contact.html'}),
    (r'^comments/',     include('django.contrib.comments.urls.comments')),
)

## Sitemap
urlpatterns += patterns('django.contrib.sitemaps.views',
    (r'^sitemap.xml$', 'sitemap', {'sitemaps': sitemaps})
)

## Redirections
urlpatterns += patterns('',
    (r'^admin/',        include('django.contrib.admin.urls')),
    (r'^journal/',      include('biologeek.redirections')),
    (r'^liens/$',       redirect('/bistrot/')),
    (r'^syndication/$', redirect('/abonnement/')),
)

## Feeds
urlpatterns += patterns('django.contrib.syndication.views',
    (r'^abonnement/(?P<url>.*)/$', 'feed',
        {'feed_dict': { 'rss': RSSFeed, 'atom': AtomFeed }}
    ),
)

## Archives and dates
urlpatterns += patterns('',
    # Archives
    (r'^archives/$',                        archives),
    # Monthly
    (r'^(?P<year>\d{4})/(?P<month>\d{2})/$',archives_month),
    # Annual
    (r'^(?P<year>\d{4})/$',                 redirect_to_archives),
)

## Ressources
urlpatterns += patterns('django.views.generic.list_detail',
    # Posts
    (r'^([-\w]+,)*[-\w]+/(?P<slug>[-\w]+)/$',
        'object_detail',
        dict(
            queryset = Post.published.all(),
            template_object_name = 'post',
            slug_field = 'slug'
        )
    ),
    # Thoughts
    (r'^\d{4}/\d{2}/(?P<slug>[-\w]+)/$',
        'object_detail',
        dict(
            queryset = Thought.published.all(),
            template_object_name = 'thought',
            slug_field = 'slug'
        )
    ),

## Agregations
    # Journal
    (r'^journal/$',
        'object_list',
        dict(
            queryset = Post.published.all()[:20],
            template_object_name = 'post'
        )
    ),
    # Bistrot
    (r'^bistrot/$',
        'object_list',
        dict(
            queryset = Thought.published.all()[:10],
            template_object_name = 'thought'
        )
    ),
    # Comments
    (r'^comments/$',
        'object_list',
        dict(
            queryset = FreeComment.objects.all()[:10],
            template_object_name = 'comment'
        )
    ),
)

## Tags (warning, order is really important! Put tags at the very end.)
urlpatterns += patterns('',
    # Tags
    (r'^(?P<tags>([-\w]+,)*[-\w]+)/$',                  view_tag),
    # Add a tag
    (r'^(?P<tags>([-\w]+,)*[-\w]+)/(?P<tag>\+[-\w]+)/$', add_tag),
)

## Media
if settings.DEBUG:
    urlpatterns += patterns('django.views.static',
        (r'^media/(?P<path>.*)$',
            'serve',
            dict(
                document_root = os.path.join(settings.PROJECT_PATH, 'media'),
                show_indexes = True
            )
        ),
    )
